﻿/***********************************************************************
 * <copyright file="ReportHelper.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Wednesday, March 05, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using WindowsForms.AOnline.Model.BusinessObjects.Report;
using WindowsForms.AOnline.Session;
using DevExpress.XtraEditors;
using MaiLinh.RSSHelper;
using PerpetuumSoft.Framework.Drawing;

namespace WindowsForms.AOnline.Report.ReportClass
{
    /// <summary>
    /// 
    /// </summary>
    public class ReportHelper:BaseReport
    {
        #region "Fields"

        /// <summary>
        /// The _o rs tool
        /// </summary>
        private ReportSharpHelper _rsTool;

        public XtraReport Report;

        /// <summary>
        /// The _report list model
        /// </summary>
        private ReportListModel _reportListModel;

        /// <summary>
        /// The _FRM parent form
        /// </summary>
        private XtraForm _frmParentForm;

        #endregion

        #region "Properties"

        /// <summary>
        /// Gets or sets the report lists.
        /// </summary>
        /// <value>
        /// The report lists.
        /// </value>
        public List<ReportListModel> ReportLists { get; set; }

        /// <summary>
        /// The common variable
        /// </summary>
        public GlobalVariable CommonVariable;

        /// <summary>
        /// Gets or sets the rs tool.
        /// </summary>
        /// <value>
        /// The rs tool.
        /// </value>
        public ReportSharpHelper ReportSharpTool
        {
            get
            {
                return _rsTool;
            }
            set
            {
                var handler1 = new ReportSharpHelper.RefreshEventHandler(RefreshReport);
                var handler2 = new ReportSharpHelper.DrilldownVoucherEventHandler(ReportDrillDownVoucher);
                var handler3 = new ReportSharpHelper.DrilldownLedgerReportHandler(ReportDrillDownLedgerReport);
                var handler4 = new ReportSharpHelper.ReportAboutEventHandler(ReportAbout);
                _rsTool = value;
                if (_rsTool == null) return;
                _rsTool.RefreshEvent += handler1;
                _rsTool.DrilldownVoucherEvent += handler2;
                _rsTool.DrilldownLedgerReportEvent += handler3;
                _rsTool.ReportAboutEvent += handler4;
            }
        }
        /// <summary>
        /// Gets or sets the parameters.
        /// </summary>
        /// <value>
        /// The parameters.
        /// </value>
        public SortedList Parameters { get; set; }

        /// <summary>
        /// Gets or sets the data member.
        /// </summary>
        /// <value>
        /// The data member.
        /// </value>
        public string DataMember { get; set; }

        #endregion

        #region "Methods"

        /// <summary>
        /// Initializes a new instance of the <see cref="ReportHelper" /> class.
        /// </summary>
        public ReportHelper()
        {
            //ReportSharpTool = new ReportSharpHelper();
        }

        /// <summary>
        /// Reports the about.
        /// </summary>
        private static void ReportAbout()
        {
        }

        /// <summary>
        /// Refreshes the report.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        private void RefreshReport(ref ICollection dataSource)
        {
            try
            {
                if (dataSource == null) return;
                if (_reportListModel == null) return;
                ReportSharpTool.IsRefresh = true;
                var data = GetDataSource(_frmParentForm, _reportListModel);
                if (data != null)
                {
                    dataSource = data;
                    if (dataSource.Count < 1)
                    {
                        XtraMessageBox.Show("Không có bản ghi nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    ReportSharpTool.IsRefresh = false;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Reports the drill down voucher.
        /// </summary>
        /// <param name="refType">The reftype.</param>
        /// <param name="refId">The refid.</param>
        private static void ReportDrillDownVoucher(string refType, string refId)
        {
            try
            {
                ReportTool.DrillDownReportVoucher(refType, refId);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Reports the drill down ledger report.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="eParam">The e parameter.</param>
        private static void ReportDrillDownLedgerReport(object sender, DrilldownReportParam eParam)
        {
            try
            {
                ReportTool.DrillDownReport(sender, eParam);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Adds the parameter.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        /// <returns></returns>
        /// ReSharper disable once UnusedMember.Local
        /// ReSharper disable once UnusedParameter.Local
        private bool AddParameter(ref ICollection dataSource)
        {
            var oRsTool = ReportSharpTool;
            if (ReportSharpTool != null)
            {
                oRsTool.Parameters.Add("CompanyDirector", CommonVariable.CompanyDirector);
                oRsTool.Parameters.Add("CompanyReportPreparer", CommonVariable.CompanyReportPreparer);
              //  oRsTool.Parameters.Add("CompanyChiefAccountant", CommonVariable.CompanyChiefAccountant);
                oRsTool.Parameters.Add("CompanyCashier", CommonVariable.CompanyCashier);
                oRsTool.Parameters.Add("CompanyStoreKeeper", CommonVariable.CompanyStoreKeeper);
            }
            else
            {
                oRsTool.Parameters.Add("CompanyDirector", "");
                oRsTool.Parameters.Add("CompanyReportPreparer", "");
                oRsTool.Parameters.Add("CompanyChiefAccountant", "");
                oRsTool.Parameters.Add("CompanyCashier", "");
                oRsTool.Parameters.Add("CompanyStoreKeeper", "");
            }

          //  oRsTool.Parameters.Add("StartDate", CommonVariable.StartDate);

            var licenseParameter = oRsTool.LicenseParameters;

            licenseParameter.Add("IsValidLicense", "false");
            licenseParameter.Add("CompanyParentName", CommonVariable.CompanyInCharge);
            var parentCompanyFont = CommonVariable.CompanyInChargeFont.Split(';');
            if (parentCompanyFont.Length != 0)
            {
                var fontstyle = (FontStyle)Enum.Parse(typeof(FontStyle), parentCompanyFont[2], true);
                var font = new Font(parentCompanyFont[0], float.Parse(parentCompanyFont[1]), fontstyle);
                licenseParameter.Add("CompanyParentNameFont", new FontDescriptor(font));
            }
            else
            {
                licenseParameter.Add("CompanyParentNameFont", new FontDescriptor("Times New Roman", 11, FontStyleMode.On, FontStyleMode.Off, FontStyleMode.Off, FontStyleMode.Off));
            }
            licenseParameter.Add("CompanyName", CommonVariable.CompanyName);


            var companyNameFont = CommonVariable.CompanyNameFont.Split(';');
            if (companyNameFont.Length != 0)
            {
                var fontstyle = (FontStyle)Enum.Parse(typeof(FontStyle), companyNameFont[2], true);
                var font = new Font(companyNameFont[0], float.Parse(companyNameFont[1]), fontstyle);
                licenseParameter.Add("CompanyNameFont", new FontDescriptor(font));
            }
            else
            {
                licenseParameter.Add("CompanyNameFont", new FontDescriptor("Times New Roman", 11, FontStyleMode.On, FontStyleMode.Off, FontStyleMode.Off, FontStyleMode.Off));
            }
            licenseParameter.Add("CompanyAddress", CommonVariable.CompanyAddress);

            var companyAddressFont = CommonVariable.CompanyAddressFont.Split(';');
            if (companyAddressFont.Length != 0)
            {
                var fontstyle = (FontStyle)Enum.Parse(typeof(FontStyle), companyAddressFont[2], true);
                var font = new Font(companyAddressFont[0], float.Parse(companyAddressFont[1]), fontstyle);
                licenseParameter.Add("CompanyAddressFont", new FontDescriptor(font));
            }
            else
            {
                licenseParameter.Add("CompanyAddressFont", new FontDescriptor("Times New Roman", 11, FontStyleMode.On, FontStyleMode.Off, FontStyleMode.Off, FontStyleMode.Off));
            }
            return true;
        }

        /// <summary>
        /// Designs the report template.
        /// </summary>
        /// <param name="reportPathFile">The sreportpathfile.</param>
        /// ReSharper disable once UnusedMember.Local
        private void DesignReportTemplate(string reportPathFile)
        {
            try
            {
                ReportSharpTool.ReportFileName = reportPathFile;
                ReportSharpTool.DesignReport();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Prints the preview report.
        /// </summary>
        /// <param name="frmParent">The FRM parent.</param>
        /// <param name="reportID">The s report identifier.</param>
        /// <param name="isPint">if set to <c>true</c> [is pint].</param>
        public void PrintPreviewReport(XtraForm frmParent, string reportID, bool isPint)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                _reportListModel = ReportLists.Find(item => item.Code == reportID);
                if (_reportListModel == null) return;
                _frmParentForm = frmParent;
                var reportListSource = GetDataSource(frmParent, _reportListModel);
                if (reportListSource == null || reportListSource.Count <= 0)
                {
                    XtraMessageBox.Show("Dữ liệu lấy lên báo cáo Không có bản ghi nào.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!string.IsNullOrEmpty(_reportListModel.TableName))
                {
                    DataMember = _reportListModel.TableName.Trim();
                }
                DisplayReport(ref reportListSource, _reportListModel, false, frmParent, false, isPint, DateTime.MinValue);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Prints the preview report by report.
        /// </summary>
        /// <param name="frmParent">The FRM parent.</param>
        /// <param name="param">The e parameter.</param>
        /// <param name="isPrint">if set to <c>true</c> [is print].</param>
        public void PrintPreviewReportByReport(XtraForm frmParent, DrilldownReportParam param, bool isPrint)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                _reportListModel = ReportLists.Find(item => item.Code == param.ArgParameter[0].ToString());
                if (_reportListModel == null) return;
                _frmParentForm = frmParent;
                var reportListSource = GetDataSource(frmParent, _reportListModel);
                if (reportListSource == null || reportListSource.Count <= 0) return;
                if (reportListSource.Count > 0)
                {
                    if (!string.IsNullOrEmpty(_reportListModel.TableName))
                    {
                        DataMember = _reportListModel.TableName.Trim();
                    }
                    DisplayReport(ref reportListSource, _reportListModel, false, frmParent, false, isPrint, DateTime.MinValue);
                }
                else
                {
                    XtraMessageBox.Show("Không có bản ghi nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Gets the data source.
        /// </summary>
        /// <param name="frmParent">The FRM parent.</param>
        /// <param name="reportList">The report list.</param>
        /// <returns></returns>
        private ICollection GetDataSource(XtraForm frmParent, ReportListModel reportList)
        {
            IList dataSource = null;
            try
            {
                if (!string.IsNullOrEmpty(reportList.InputTypeName))
                {
                    var type = Assembly.GetExecutingAssembly().GetType((GetType().Namespace + "." + reportList.InputTypeName));
                    var target = (BaseReport)Activator.CreateInstance(type);
                    if (!string.IsNullOrEmpty(reportList.ProcedureReportName))
                    {
                        if (CommonVariable == null)
                        {
                            CommonVariable = new GlobalVariable();
                        }
                        CommonVariable.ReportList = reportList;
                        var args = new object[] { frmParent, CommonVariable, _rsTool };
                        dataSource = (IList)(type.InvokeMember(reportList.FunctionReportName, BindingFlags.InvokeMethod, null, target, args));
                    }
                }

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return dataSource;
        }

        /// <summary>
        /// Displays the report.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        /// <param name="reportList">The report list.</param>
        /// <param name="isVoucher">if set to <c>true</c> [b is voucher].</param>
        /// <param name="frmForm">The   form.</param>
        /// <param name="isShowDialog">if set to <c>true</c> [b show dialog].</param>
        /// <param name="isPrint">if set to <c>true</c> [is print].</param>
        /// <param name="voucherDate">The d voucher date.</param>
        public void DisplayReport(ref ICollection dataSource, ReportListModel reportList, bool isVoucher, XtraForm frmForm, bool isShowDialog, bool isPrint, DateTime voucherDate)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (dataSource == null) return;
                CommonVariable = new GlobalVariable();
                //var str = CommonVariable.ReportPath + reportList.File;
                //var oRsTool = ReportSharpTool;
                //oRsTool.RssObject.VoucherDate = voucherDate;
                //oRsTool.ListDataSource = dataSource;
                //oRsTool.DataMember = reportList.TableName.Trim();
                //oRsTool.LayoutReportPath = CommonVariable.ReportPath;
                //oRsTool.ReportFileName = str;
                ////if (!AddParameter(ref dataSource)) return;
                //oRsTool.IsPrint = isPrint;
                //oRsTool.ReportTitle = reportList.Name;
                //oRsTool.ProductName = CommonVariable.ProducName;
                //oRsTool.DisplayProductName = true;

                
                //_frmParentForm = frmForm;
                //oRsTool.RunReport(frmForm, isShowDialog);
                //Report = new XtraReport();
                Report.DataSource = dataSource;
                Report.ShowPreview();
                //PreviewReport(str);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        #endregion
    }
}
